package org.test;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @Author: wjy
 * @Date: 2022/2/15 9:43
 */
public class Demo2 {

    public static void main(String[] args) {

        /*Calendar beginTime = Calendar.getInstance();
        beginTime.set(2021, 1, 1, 0, 0, 0);*/
        Date beginTime = new Date(2021, 1, 1, 0, 0, 0);

        /*Calendar endTime = Calendar.getInstance();
        endTime.set(2022, 12, 31, 0, 0, 0);*/
        Date endTime = new Date(2022, 12, 1, 0, 0, 0);

        Date subTime = new Date(2021, 12, 1, 0, 0, 0);

        Instant instant = beginTime.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        System.out.println("beginTime = " + beginTime);
        System.out.println("LocalDateTime = " + localDateTime);

        LocalDateTime date = LocalDateTime.of(2021, 12, 1, 22, 32, 10);
        System.out.println(date);

        System.out.println();

        System.out.println(getDifMonth(beginTime, endTime));
/*
        System.out.println();

        System.out.println(calDiffMonth(beginTime, endTime));*/

        Map<Integer, Object> map = new HashMap<>();
        map.put(1, "1");
        map.put(2, "2");
        map.put(3, "3");
        map.put(4, "4");
        map.put(5, "5");

        map.forEach((k, v) -> {
            System.out.println(k + "============" + v);
        });

        System.out.println(14 / 12);

    }

    public static Integer getDifMonth(Date startDate, Date endDate){
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(startDate);
        end.setTime(endDate);

        int monthNum = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12 + (end.get(Calendar.MONTH) - start.get(Calendar.MONTH)) + 1;
        System.out.println(monthNum);

/*        System.out.println(end.get(Calendar.MONTH));
        System.out.println(start.get(Calendar.MONTH));

        System.out.println(end.get(Calendar.DAY_OF_MONTH));*/

/*        int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
        System.out.println(result);
        int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
        return Math.abs(month);*/
        return 0;
    }

    //获取哪一天
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    /**
     * 返回日期的月份，1-12,即yyyy-MM-dd中的MM
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 返回日期的年,即yyyy-MM-dd中的yyyy
     *
     * @param date
     *            Date
     * @return int
     */
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
    public static int calDiffMonth(Date start, Date end) {
        int result=0;
        try {
            //SimpleDateFormat sfd=new SimpleDateFormat("yyyyMMdd");
//            Date start = sfd.parse(startDate);
//            Date end = sfd.parse(endDate);

            int startYear=getYear(start);
            int startMonth=getMonth(start);
            int startDay=getDay(start);
            int endYear=getYear(end);
            int endMonth=getMonth(end);
            int endDay=getDay(end);

            Calendar calendar2= Calendar.getInstance();
            calendar2.setTime(start);
            int maxDay = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);//获取起始日期所在月的最后一天
            Calendar calendar3 = Calendar.getInstance();
            calendar3.setTime(end);
            int maxEndDay = calendar3.getActualMaximum(Calendar.DAY_OF_MONTH);//获取结束日期所在月的最后一天
            if(startDay == maxDay){//起始日期是在月末
                if(maxEndDay==endDay){
                    result=(endYear-startYear)*12+endMonth-startMonth;
                }else {
                    result=(endYear-startYear)*12+endMonth-startMonth-1;
                }
            }else if(endDay==maxEndDay){//结束日期是在月末
                result=(endYear-startYear)*12+endMonth-startMonth;
            }else {
                if (endDay >= startDay) {
                    result=(endYear-startYear)*12+endMonth-startMonth;
                }else {
                    result=(endYear-startYear)*12+endMonth-startMonth-1;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
